<form class="layui-form seller-form" action="">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">部/院：</label>
            <div class="layui-input-inline" id="dep_box">
                <select name="dep_id" lay-filter="dep_id" id="dep_id">
                    <option value=''>请选择</option>
                    {volist name="depList" id="vo"}
                    <option value="{$vo.bmdm}">{$vo.bmmc}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">专业：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="specialty_id" lay-filter="specialty_id" id="specialty_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">班级：</label>
            <div class="layui-input-inline" id="class_box">
                <select name="class_id" lay-filter="class_id" id="class_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">方向：</label>
            <div class="layui-input-inline" id="dir_box">
                <select name="dir" lay-filter="dir" id="dir" lay-search>
                    <option value=""></option>
                    <option value="1">就业</option>
                    <option value="2">升学</option>
                    <option value="3">创业</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">类型：</label>
            <div class="layui-input-inline">
                <select id="type_id" lay-filter="type_id" name="type_id">
                    <option value=''>请选择</option>
                    {volist name="typeList" id="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">参数：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="param_id" lay-filter="param_id" id="param_id" lay-search>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">学号：</label>
            <div class="layui-input-inline" id="xh_box">
                <input type="text" id="xh" name="xh" class="layui-input" value="" placeholder="请输入学号" ></input>
            </div>
        </div> 
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">姓名：</label>
            <div class="layui-input-inline">
                <input id="xm" type="text" name="xm" lay-verify="xm" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">辅导员：</label>
            <div class="layui-input-inline">
                <input type="text" id="teacherName" name="teacherName" lay-verify="title" placeholder="请输入辅导员姓名" autocomplete="off" class="layui-input">
            </div>
        </div> 
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">是否就绪：</label>
            <div class="layui-input-inline" id="status_box">
                <select name="status" lay-filter="status" id="status" lay-search>
                    <option value=""></option>
                    <option value="0">未就绪</option>
                    <option value="1">已就绪</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i
                    class="iconfont icon-chaxun"></i>查询</button>
            <button id="exportBtn" class="layui-btn layui-btn-sm layui-btn-disabled" lay-submit lay-filter="export-student"><i
                    class="iconfont icon-clouddownload" style="font-size: 20px !important;" disabled></i>导出</button> 
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-student"><i
                    class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>导入</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-student-type"><i
                class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>类型参数导入</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-teacher"><i
                    class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>导入（辅）</button>
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="sync" id="sync"><i
                        class="iconfont icon-chaxun"></i>同步财务数据</button>    
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="batch-replace"><i
                    class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>批量替换辅导员</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="studentTable" lay-filter="studentTable"></table>
</div>

<div id="exportStudent" style="display: none;">
    <form class="layui-form export-form" action="">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的学生，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>


<div id="importStudent" style="display: none;">
    <form class="layui-form import-form" method="post" action="{:url('ietask/import')}" enctype="multipart/form-data">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先下载<a href="{:url('ietask/importTemplete',['tplName'=>'student'])}">导入模板(不支持迅雷等下载工具)</a>
                </blockquote>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">
                    <input type="file" class="layui-file" name="importFile" id="importFile">
                    <input type="hidden" name="model" value="Student">
                </label>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function () {
        var layer = layui.layer, table = layui.table, form = layui.form, date = layui.laydate;
        //执行渲染
        table.render({
            elem: '#studentTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-195',
            cellMinWidth: '80',
            page: 'true',
            limit: '20',
            id: 'studentTable',
            url: "{:url('BookCommon/returnNull')}",
            cols: [[ //标题栏
                { type: 'numbers' },    
                { field: 'bmmc', sort: true, title: '部/院', width: 200, align: 'center' },
                { field: 'zymc', sort: true, title: '专业', width: 200, align: 'center' },
                { field: 'bjmc', sort: true, title: '班级', width: 200, align: 'center' },
                { field: 'dirName', sort: true, title: '方向', width: 80, align: 'center' },
                { field: 'xh', sort: true, title: '学号', width: 120, align: 'center' },
                { field: 'xm', sort: true, title: '姓名', width: 200, align: 'center' },
                { field: 'mobile', sort: true, title: '辅导员手机', width: 160, align: 'center' },
                { field: 'truename', sort: true, title: '辅导员', width: 100, align: 'center' },
                { width: 80, title: '操作', align: 'center', toolbar: '#hooksBar' }
            ]], //设置表头
            where: { "year": $("#year").val(), "season": $("#season").val(), "dir": $("#dir").val() },
            done: function(res, curr, count){
                // 判断加载结果数量是否大于0
                if(count>0){
                    $("#exportBtn").removeClass("layui-btn-disabled");
                    $('#exportBtn').removeAttr("disabled");
                } else {
                    $('#exportBtn').addClass("layui-btn-disabled");
                    $('#exportBtn').attr("disabled", true);
                }
            }
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        // 查询
        form.on('submit(search)', function (data) {
            table.reload('studentTable', {
                where: data.field,
                page: {curr: 1},
                url: "{:url('student/index')}?dep_id={$bmdm}",
            });

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
 
        
        // 联动专业
        layui.use('form', function () {
            form.on('select(dep_id)', function (data) {
                getSpecialty(data);
            });
            
            form.on('select(specialty_id)', function (data) {
                getClass(data);
            });
            
            // 联动参数
            form.on('select(type_id)', function (data) {
                getParams(data);
            });
        });
        
        // 得到专业
        function getSpecialty(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getSpecicalties')}",
                data: 'bmdm=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#specialty_id").empty();//清空下拉框的值
                        $('#specialty_id').append(new Option('', ''));// 下拉菜单里添加元素
                        
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data.data, function (index, item) {
                            $('#specialty_id').append(new Option(item.zymc, item.zydm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };
        
        // 得到班级
        function getClass(data) { 
            var dept = $("#dep_id").val();
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getCWClasses')}",
                data: {
                        "bmdm": dept,
                        "zydm": data.value
                    },
                success: function(e) {
                    if(e.status) {
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data, function (index, item) {
                            $('#class_id').append(new Option(item.bjmc, item.bjdm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };
        
        // 得到参数
        function getParams(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getParams')}",
                data: 'type_id=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#param_id").empty();//清空下拉框的值
                        $('#param_id').append(new Option('', ''));// 下拉菜单里添加元素 
                        $.each(e.data, function (index, item) {
                            $('#param_id').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

        //监听工具条
        table.on('tool(studentTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            
            if(layEvent === 'edit'){ //编辑 
                var user_id = $(this).attr('data-id');
                var url = "{:url('Student/edit')}?xh=" + data.xh;
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                topLayui.index.openTabsPage(url, '编辑学生信息'); 
            }
        });

        //学生信息导出
        layui.form.on('submit(export-student)', function (data) {
            layer.open({
                type: 1,
                title: '学生信息导出',
                area: ['400px', '248px'], //宽高
                btn: ['确定', '取消'],
                content: $("#exportStudent").html(),
                success: function () {
                    $(':focus').blur();
                    this.enterEsc = function(event){
                        if(event.keyCode === 13){
                            if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                                $(".layui-layer-btn0").addClass("layui-btn-disabled");

                                //判断是否有选中
                                var filter = $(".seller-form").serialize();
                                $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                                var data = $(".export-form:last").serializeArray();
                                data.push({ 'name': 'model', 'value': 'Student' });

                                if($.trim(data[0]['value']) == '') {
                                    layer.msg('请填写任务名称！', { time: 1300 });
                                    $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    return false;
                                }

                                JsPost("{:url('Ietask/export')}", data, function (res) {
                                        layer.msg(res.msg, { time: 1500 }, function () {
                                            if (res.status) {
                                                layer.closeAll();
                                                //执行跳转
                                                var topLayui = parent === self ? layui : top.layui;
                                                topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                            } else {
                                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                            }
                                        });
                                    }
                                );
                            }
                            return false; //阻止系统默认回车事件
                        }
                    };
                    $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                },
                yes: function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        //判断是否有选中
                        var filter = $(".seller-form").serialize();
                        $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                        var data = $(".export-form:last").serializeArray();
                        if($.trim(data[0]['value']) == '') {
                            layer.msg('请填写任务名称！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        data.push({ 'name': 'model', 'value': 'Student' });
                        JsPost("{:url('Ietask/export')}", data, function (res) {
                                layer.msg(res.msg, { time: 1500 }, function () {
                                    if (res.status) {
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                    } else {
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                }, btn2: function () {
                    layer.closeAll();
                }, end: function(){
                    $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //导入学生信息
        layui.form.on('submit(import-student)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Student/importStudent")}', '导入学生信息'); 
            return false;
        }); 

        //导入学生类型参数
        layui.form.on('submit(import-student-type)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Student/importStudentType")}', '导入学生类型参数'); 
            return false;
        }); 
        
        //按班级导入辅导员
        layui.form.on('submit(import-teacher)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Student/importTeacher")}', '按班级导入辅导员'); 
            return false;
        }); 
        
        //批量替换辅导员
        layui.form.on('submit(batch-replace)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Student/batchReplaceIndex")}', '批量替换辅导员'); 
            return false;
        }); 

        layui.form.on('submit(sync)', function (data) {
            $("#sync").addClass("layui-btn-disabled");
            $('#sync').attr("disabled", true);
            JsGet("{:url('Student/sync')}", function(res){
                $("#sync").removeClass("layui-btn-disabled");
                $('#sync').removeAttr("disabled");
                layer.msg(res.msg);
            });
            return false;
        });
    });
</script>

<script type="text/html" id="hooksBar">
    <a class="layui-btn layui-btn-xs" name="edit" lay-event="edit">编辑</a>
</script>